package com.gy.assistant.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;

import java.util.ArrayList;
import java.util.Arrays;

import im.zego.zim.callback.ZIMGroupCreatedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMErrorUserInfo;
import im.zego.zim.entity.ZIMGroupFullInfo;
import im.zego.zim.entity.ZIMGroupInfo;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.BaseActivity;

public class ZIMGroupChatCreateActivity extends BaseActivity {

    private TextView groupIDText;
    private TextView groupNameText;
    private TextView groupMemberUsersText;
    private EditText groupAvatarText;
    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, ZIMGroupChatCreateActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_chat_create);
        groupIDText = findViewById(R.id.group_id);
        groupNameText = findViewById(R.id.group_name);
        groupMemberUsersText = findViewById(R.id.group_member_users);
        groupAvatarText = findViewById(R.id.group_avatar_users);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onCreateGroupChat(View view) {
        String groupID = groupIDText.getText().toString();
        String groupName = groupNameText.getText().toString();
        String memberID = groupMemberUsersText.getText().toString();
        String avatar = groupAvatarText.getText().toString();
        String[] memberUsersList = memberID.split(";");
        ZIMGroupInfo groupInfo = new ZIMGroupInfo();
        groupInfo.groupID = groupID;
        groupInfo.groupName = groupName;
        groupInfo.groupAvatarUrl = avatar; //
        SDKManager.sharedInstance().createGroup(groupInfo, new ArrayList<>(Arrays.asList(memberUsersList)), new ZIMGroupCreatedCallback() {
            @Override
            public void onGroupCreated(ZIMGroupFullInfo groupInfo, ArrayList<ZIMGroupMemberInfo> userList, ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ZIMGroupChatCreateActivity.this, getString(R.string.create_group_failed) + errorInfo.code, Toast.LENGTH_LONG).show();
                    return;
                }
                if (userList.size() == 0) {
                    Toast.makeText(ZIMGroupChatCreateActivity.this, getString(R.string.create_group_failed)+ getString(R.string.no_member), Toast.LENGTH_LONG).show();
                } else if (errorUserList.size() > 0) {
                    Toast.makeText(ZIMGroupChatCreateActivity.this, getString(R.string.group_user_add_failed), Toast.LENGTH_SHORT).show();
                }
                ZIMGroupChatActivity.actionStart(ZIMGroupChatCreateActivity.this, groupInfo.baseInfo.groupID, groupName,"");
            }
        });
    }

}
